package cn.tedu.straw.portal.controller;


import cn.tedu.straw.portal.dto.StudentRegisterDTO;
import cn.tedu.straw.portal.ex.*;
import cn.tedu.straw.portal.service.IUserService;
import cn.tedu.straw.portal.util.R;
import cn.tedu.straw.portal.vo.TeacherVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-10-15
 */
@RestController
@RequestMapping("/portal/users")
public class UserController {
    @Autowired
    IUserService service;

    @RequestMapping("/reg/student")
    public R studentRegister(@Valid StudentRegisterDTO studentRegisterDTO, BindingResult bindingResult){

        // R r=new R();
        // http://localhost:8080/portal/user/reg/student?inviteCode=JSD2006-888888&nickname=Tom&phone=13800000100&password=123456

        if(bindingResult.hasErrors()){
            String errorMessage=bindingResult.getFieldError().getDefaultMessage();
            throw new ParamValidationException(errorMessage);
           //return R.failure(R.State.ERR_PARAM_VALIDATION,errorMessage);
        }
       /* try{
            service.studentRegister(studentRegisterDTO);
            r.setStatus(1);
            r.setMessage("注册成功!");
        }catch(InviteCodeException e){
            r.setStatus(2);
            r.setMessage("邀请码错误!");
        }catch(ClassDisabledException e){
            r.setStatus(3);
            r.setMessage("班级号被禁用!");
        }catch(PhoneDuplicateException e){
            r.setStatus(4);
            r.setMessage("手机号被注册!");
        }catch(InsertException e){
            r.setStatus(5);
            r.setMessage("服务器忙");
        }*/
        service.studentRegister(studentRegisterDTO);
        return R.ok();
    }
    @GetMapping("/teachers")
    public R getTeacherList() {
        List<TeacherVO> teachers = service.getTeacherList();
        return R.ok(teachers);
    }


}
